/*************************************************************************
 *************************************************************************
 	 	 	 	 	 	 	M2SMA : DWGraphicAgent.cpp
 *************************************************************************
 *
 	 Copyright (C) 2011
 	 DW-2011, Steven Costiou, France
 *
 **************************************************************************
 **************************************************************************
 *
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/
 *
 **************************************************************************
 **************************************************************************/

#include "DWGraphicAgent.h"
#include "GameEngine.h"

DWGraphicAgent::DWGraphicAgent(Engine* anEngine) : Basic2DGraphicAgent(anEngine)
{
	this->addClassName("DWGraphicAgent");
	filename = "/home/rstv/workspace/Darth-Wouf-2011/graphics/pomme.png"; /// FIXME String utilisée uniquement pour du test sur la superclasse.
	initialize();
	initializeGraphics();
	display();
}

DWGraphicAgent::~DWGraphicAgent()
{
	cleanUpSDLObjects();
	/// TODO Faire une passe sur tous les destructeurs !
}

DWGraphicAgent::DWGraphicAgent(const DWGraphicAgent& aDWGraphicAgent, Engine* anEngine)
: Basic2DGraphicAgent(aDWGraphicAgent, anEngine)
{ /// TODO retravailler dans le détail les constructeurs par recopie !
	selfCopyFrom(aDWGraphicAgent, anEngine);
	/// FIXME Est-ce que l'agent devrait s'ajouter lui même au système ?
}

void DWGraphicAgent::initialize()
{
	blitSurface = NULL;
	graphicImage = NULL;
}

void DWGraphicAgent::initializeGraphics()
{
	loadImage(filename);
}

void DWGraphicAgent::cleanUpSDLObjects()
{
	if(blitSurface != NULL)
		delete blitSurface;

	if(graphicImage != NULL)
		SDL_FreeSurface(graphicImage);
}

void DWGraphicAgent::selfCopyFrom(const Basic2DGraphicAgent& anAgent, Engine* anEngine)
{
	/// TODO
}

bool DWGraphicAgent::isEqualTo(const Basic2DGraphicAgent& anAgent) const
{
	/// TODO
	return false;
}

SDL_Surface* DWGraphicAgent::getCurrentGraphics()
{
	return graphicImage;
}

void DWGraphicAgent::move()
{
	/// TODO déplacer dans la sous classe "movable"
	Basic2DGraphicAgent::move();
}

void DWGraphicAgent::loadImage(const string filename)
{
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load(filename.c_str());

    if(loadedImage != NULL)
    {
    	optimizedImage = SDL_DisplayFormat(loadedImage);

        SDL_FreeSurface(loadedImage);

        /**
         * FIXME Is that needed accounted that Yann already managed transparency in his sprites ?
         */
       if(optimizedImage != NULL)
       {
           SDL_SetColorKey(optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0xFF, 0xFF, 0xFF));
       }
    }

    graphicImage = optimizedImage;
}

void DWGraphicAgent::display()
{
	unsigned long int anId;

	GameEngine* gameEngine = (GameEngine*) getParentEngine();
	anId = gameEngine->requestSubscriptionForGraphicDisplay(this);

	setGraphicRegistrationId(anId);
}

void DWGraphicAgent::live()
{
	cout << "This is DW Graphic Agent : " << this->getName() << endl;
	move();

	if(agentIsOutOfBounds())
	{
		generateRandomcoordinates();
		selectRandomDirection();
		//coordinates->h = 5;
		//coordinates->w = 5;
	}

}

